package com.xqboss.apps.service.equipment;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xqboss.apps.domain.equipment.Equipment;
import com.xqboss.apps.domain.equipment.EquipmentUpgradeConfig;
import com.xqboss.apps.domain.equipment.EquipmentUpgradeConfigCoefficient;
import com.xqboss.apps.domain.nft.Nft;
import com.xqboss.apps.enums.equipment.UpgradeConfigCoefficientEnum;
import com.xqboss.apps.mapper.equipment.EquipmentUpgradeConfigMapper;
import com.xqboss.apps.service.nft.NftService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * 装备升级加点-配置接口
 *
 * @author wangcong
 * @date 2023-11-23
 */
@Service
public class EquipmentUpgradeConfigService extends ServiceImpl<EquipmentUpgradeConfigMapper, EquipmentUpgradeConfig> {

    @Lazy
    @Autowired
    private NftService nftService;
    @Autowired
    private EquipmentUpgradeConfigCoefficientService equipmentUpgradeConfigCoefficientService;

    /**
     * 根据装备获取升级配置
     * 最小等级 <= 当前装备等级 and 最大等级 > 当前装备等级
     */
    public EquipmentUpgradeConfig getUpgradeConfigByEquipment(Equipment equipment){
        // 获取NFT的升级模板
        Nft nft = nftService.getNft(equipment.getNftId());
        EquipmentUpgradeConfig config = getOne(new LambdaQueryWrapper<EquipmentUpgradeConfig>()
                .le(EquipmentUpgradeConfig::getMinLevel, equipment.getLevel())
                .gt(EquipmentUpgradeConfig::getMaxLevel, equipment.getLevel())
                .eq(EquipmentUpgradeConfig::getUpgradeTemplateId, nft.getUpgradeId()));
        return config;
    }

    /**
     * 根据装备获取升级配置 当装备升到最大等级时，倒序获取最大等级的升级模式
     * 最小等级 <= 当前装备等级 and 最大等级 >= 当前装备等级
     */
    public EquipmentUpgradeConfig getUpgradeConfigByEquipmentMaxLevel(Equipment equipment){
        // 获取NFT的升级模板
        Nft nft = nftService.getNft(equipment.getNftId());
        EquipmentUpgradeConfig config = getOne(new LambdaQueryWrapper<EquipmentUpgradeConfig>()
                .le(EquipmentUpgradeConfig::getMinLevel, equipment.getLevel())
                .ge(EquipmentUpgradeConfig::getMaxLevel, equipment.getLevel())
                .eq(EquipmentUpgradeConfig::getUpgradeTemplateId, nft.getUpgradeId())
                .orderByDesc(EquipmentUpgradeConfig::getId)
                .last("limit 1"));
        return config;
    }

    /**
     * 根据升级配置id，检测是否可以特权升级
     * 只有当后台配置的瑕疵率为【100%且唯一】并且升级最低经验比例为0时
     */
    public boolean checkPrivilegeUpgrade(Long upgradeConfigId) {
        //获取升级配置
        EquipmentUpgradeConfig config = getById(upgradeConfigId);
        //如果配置不存在返回false
        if (config == null) {
            return false;
        }
        //获取升级配置的瑕疵率
        List<EquipmentUpgradeConfigCoefficient> coefficient = equipmentUpgradeConfigCoefficientService.getCoefficientByUpgradeConfigId(upgradeConfigId, UpgradeConfigCoefficientEnum.BLEMISH);
        //如果瑕疵率为空返回false
        if (coefficient == null || coefficient.isEmpty()) {
            return false;
        }
        //如果瑕疵率为100%(即爆率、概率 = 1)且唯一，并且升级最低经验比例为0，则可以特权升级
        return coefficient.size() == 1 && coefficient.get(0).getBurstRate().compareTo(BigDecimal.ONE) == 0
                && coefficient.get(0).getProbability().compareTo(BigDecimal.ONE) == 0
                && config.getMinExpRatio().compareTo(BigDecimal.ZERO) == 0;
    }

    /**
     * 获取升级配置的最高等级
     */
    public Integer getMaxLevelByEquipment(Equipment equipment) {
        // 获取NFT的升级模板
        Nft nft = nftService.getNft(equipment.getNftId());
        EquipmentUpgradeConfig config = getOne(new LambdaQueryWrapper<EquipmentUpgradeConfig>()
                .eq(EquipmentUpgradeConfig::getUpgradeTemplateId, nft.getUpgradeId())
                .orderByDesc(EquipmentUpgradeConfig::getMaxLevel)
                .last("limit 1"));
        return config.getMaxLevel();
    }
}
